package cn.shop.dys.util;

import android.content.Context;
import android.text.TextUtils;

import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.shop.dys.bean.Msg;
import cn.shop.lib_common.CommonUtil;


/**
 * Created by jian on 2016/4/7.
 */
public class ConnectionManager implements PacketFilter, PacketListener {
    private String HOST = DEF_HOST;
    private static final String DEF_HOST = "192.168.56.1";
    private static final int PORT = 5222;
    private XMPPConnection mXmppConnection;

    private ConnectionManager() {
    }

    private static ConnectionManager sInstance = new ConnectionManager();

    public static ConnectionManager getInstance() {

        return sInstance;
    }

    public void connect(String ip) throws XMPPException {
        if (!TextUtils.isEmpty(ip)) {
            HOST = ip;
        }
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(HOST, PORT);
        connectionConfiguration.setSASLAuthenticationEnabled(false);
        mXmppConnection = new XMPPConnection(connectionConfiguration);
        mXmppConnection.connect();
    }

    public void login(Context context, String username, String password) throws XMPPException {
        mXmppConnection.login(username, password);
        if (mXmppConnection.isAuthenticated()) {
            MyDBUtil.initDB(context, CommonUtil.hanziTextToPinyinFormat(mXmppConnection.getUser()));
            mXmppConnection.addPacketListener(this, this);
        }
    }

    public boolean isConnected() {
        return mXmppConnection != null && mXmppConnection.isConnected();
    }

    public boolean isLogin() {
        return mXmppConnection.isAuthenticated();
    }

    public String getUerName() {
        String user = mXmppConnection.getUser();
        String userName = user.split("@")[0];
        return userName;
    }

    public void disconnect() {
        mXmppConnection.disconnect();
    }

    public Roster getRoster() {
        return mXmppConnection.getRoster();
    }

    public ChatManager getChatManager() {
        return mXmppConnection.getChatManager();
    }

    @Override
    public boolean accept(Packet packet) {
        return packet instanceof Message;
    }

    @Override
    public void processPacket(Packet packet) {

        Message message = (Message) packet;
        if(message.getBody()==null)return;
        String messageFrom = message.getFrom();
        String comeMsgJid = getComeMsgJidByMessageFrom(messageFrom);
        Msg msg = Msg.creatMsg(comeMsgJid, message.getBody(), Msg.CHAT_TYPE_OTHER);
        MyDBUtil.saveComeMsg(msg);
        for (MsgObserver msgObserver : mMsgObservers) {
            msgObserver.notify(msg);
        }
        MsgObserver msgObserver = mChatByJidMsgObservers.get(comeMsgJid);
        if (msgObserver != null) {

            msgObserver.notify(msg);
        }

    }

    public interface MsgObserver {

        public void notify(Msg msg);
    }

    private List<MsgObserver> mMsgObservers = new ArrayList<MsgObserver>();

    public void addMsgObserver(MsgObserver msgObserver) {

        this.mMsgObservers.add(msgObserver);
    }

    public void removeMsgObservers(MsgObserver msgObserver) {

        this.mMsgObservers.remove(msgObserver);
    }

    private Map<String, MsgObserver> mChatByJidMsgObservers = new HashMap<String, MsgObserver>();

    public void addChatMsgObserver(String userJid, MsgObserver msgObserver) {
        mChatByJidMsgObservers.put(userJid, msgObserver);
    }

    public void removeChatMsgObserver(String userJid) {

        mChatByJidMsgObservers.remove(userJid);
    }

    private String getComeMsgJidByMessageFrom(String messageFrom) {

        int end = messageFrom.lastIndexOf("/");
        return messageFrom.substring(0, end);
    }
}
